package com.open.capacity.workflow.controller;



import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow.model.WFProcessinstance;
import com.open.capacity.workflow.service.WFProcessinstanceService;
import com.open.capacity.workflow.service.WFTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 作者 ftquan 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 流程监控
 */
@RestController
@Api(tags = "流程监控模块api")
public class WFMonitorController {

    @Autowired
    private WFProcessinstanceService processinstanceService;


    /**
     * 查询流程监控
     *
     * @param keyword
     * @param startTime
     * @param endTime
     * @param categoryId
     * @return
     */
    @PreAuthorize("hasAuthority('wfmonitor:get/wfmonitors')")
    @ApiOperation(value = "查询流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/wfmonitors")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public PageResult<WFProcessinstance> findTasks(@RequestParam(value = "page") Integer page,
                                                   @RequestParam(value = "limit") Integer limit,
                                                   @RequestParam(value = "keyword", required = false) String keyword,
                                                   @RequestParam(value = "startTime", required = false)
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                                   @RequestParam(value = "endTime", required = false)
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                                   @RequestParam(value = "categoryId", required = false) String categoryId) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("createUserid", loginUser.getId());
        params.put("page", page);
        params.put("limit", limit);
        switch (categoryId) {
            case "1":
                return processinstanceService.pageProcessinstances(params);
            case "2":
                return processinstanceService.pageProcessinstances(params);
            case "3":
                return processinstanceService.pageProcessinstances(params);
            default:
                return processinstanceService.pageProcessinstances(params);
        }
    }
}
